using ED.Common.BaseModel;
using ED.Common.Extensions;
using ED.Entity;
using ED.IRepository;
using ED.IService;
using ED.Model.Request;
using ED.Model.Response;

/// <summary>
/// UserRole服务
/// </summary>
namespace ED.Service
{
    public class UserRoleService : BaseService, IUserRoleService
    {
        private readonly IUserRoleRepository userroleRepository;
        public UserRoleService(IUserRoleRepository userroleRepository)
        {
            this.userroleRepository = userroleRepository;
        }

        /// <summary>
        /// UserRole添加
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Add(int req)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// UserRole删除
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Delete(int req)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// UserRole查询
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PageOut<UserRoleRes> Page(UserRoleReq req)
        {
          var res = Orm.Select<Base_UserEntity, Base_UserRoleEntity, Base_RoleEntity>()
                .LeftJoin((user, userRole, role) => user.Id == userRole.UserId)
                .LeftJoin((user, userRole, role) => role.Id == userRole.RoleId)
                .WhereIf(req.UsertId > 0, (user, userRole, role) => user.Id == req.UsertId)
                .WhereIf(req.UserName.IsNotNullOrEmptyOrWhiteSpace(), (user, userRole, role) => user.UserName == req.UserName)
                .WhereIf(req.RoleName.IsNotNullOrEmptyOrWhiteSpace(), (user, userRole, role) => role.Name == req.RoleName)
                .WhereIf(req.RoleId > 0, (user, userRole, role) => role.Id == req.RoleId)
                .OrderBy((user, userRole, role) => user.Id)
                .Page(req.PageIndex, req.PageSize)
                 .ToList((a, b, c) => new { a.UserName, a.Id, c.Name, c.Active, c.Description, c.Code, RoleId = c.Id })
                .ToLookup(x => new { x.UserName, x.Id }).Select(x => new UserRoleRes() { UserName = x.Key.UserName, UsertId = x.Key.Id, userRoles = x.ToList().Mapper<List<UserRole>>()});
            return new PageOut<UserRoleRes> { TotalCount = res.ToList().Count, List = res.ToList() };
        }

        /// <summary>
        /// UserRole更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Update(int req)
        {
            throw new NotImplementedException();
        }
    }
}
